<template>
  <div>
    <p-directory-tree
      multiple  
      :tree-data="treeData"
      :default-expanded-keys="[]"
      :default-selected-keys="[]"
      :default-checked-keys="[]"
      :replace-fields="replaceFields"
      @select="onSelect"
      @expand="onExpand"
    />
  </div>
</template>
<script>
import Vue from "vue";
import { postAction, getAction, deleteAction } from "@/api/manage";

const treeData = [
  {
    name: "",
    key: "",
    child: [
      {
        name: "",
        key: "",
        disabled: true,
        child: [
          { name: "", key: "", },
          { name: "", key: "" },
        ],
      },
      {
        name: "",
        key: "",
        child: [{ key: "", name: "" }],
      },
    ],
  },
];

export default {
  props:['SearchValue'],
  data() {
    return {
      treeId:[],
      //分页获取生产排班请求参数
      query: {
        keyword: "",
        layoutState: "",
        layoutTime: "",
        limit: 10,
        lineno: "",
        pageNo: 1,
        workroomno: "",
      },
      //新增规则
      addParam:{
          code:"",
          content:"",
          name:"",
          ruleld:"",
          scope:"",
          status:""
      },
      treeData,
      replaceFields: {
        children: "child",
        title: "name",
      },
      url: {
        productionRulesList: "/mes-manufacture/mfRule/list",
      },
    };
  },
  mounted(){
  },
  created() {
    this.initData();
  },
  methods: {
    onSelect(selectedKeys, info) {
      console.log( selectedKeys, info);
      this.$emit('onSelect',info.node.dataRef || {});
    },
    onCheck(checkedKeys, info) {
      console.log("onCheck", checkedKeys, info);
    },
    //TreeData
    initData() {
      getAction(this.url.productionRulesList, this.query).then((res) => {
        if (res.success) {
          // this.$message.success(res.msg);
          this.treeData = res.data;
          this.showData=res.data;
        }
        if (res.code === 510) {
          this.$message.warning(res.msg);
        }
      });
      
    },
  },
  watch:{
    SearchValue(){
      if(this.SearchValue){
        let newData=[]
        let parentData=[]
        this.showData.forEach(item => {
          if(item.child.length>0){
            newData.push(...item.child.filter(items=>{return items.name.indexOf(this.SearchValue)!=0}))
          }else{
            parentData=this.showData.filter(item=>{return item.name.indexOf(this.SearchValue)!=0})
          }
        });
        this.treeData=newData
      }else{
        this.treeData= this.showData
      }
    }
  }
};
</script>
<style scoped lang="less">
</style>